﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameEngine.Scenes
{
    public class SceneManager
    {
        #region SingletonPattern
        //Singleton Pattern
        private static SceneManager instance;
        private SceneManager() { }
        public static SceneManager getInstance()
        {
            if (instance == null)
            {
                instance = new SceneManager();
            }
            return instance;
        }
        #endregion
        #region Attributes
        //Attributes
        public List<Scene> scenes = new List<Scene>();
        public List<Scene> markedForRemove;
        public List<Scene> markedForAdd;
        public bool canAdd = true, canRemove = true;
        public bool enabledInput = true;
        #endregion
        #region Functionality
        public void update(GameTime gameTime)
        {
            int i = scenes.Count - 1;
            bool inputHandeled = false;
            canRemove = false;
            while (i >= 0)
            {
                scenes[i].update(gameTime);
                InputManager.getInstance().update(gameTime);
                if (enabledInput && !inputHandeled) inputHandeled = handleInput();
                if (scenes[i].doesHalt) //it pauses the scenes from behind
                    break;
                i--;
            }
            canRemove = true;
            if (markedForRemove != null) 
            {
                foreach (Scene s in markedForRemove)
                {
                    scenes.Remove(s);
                }
                markedForRemove = new List<Scene>();
            }
        }
        public void draw(SpriteBatch sb)
        {
            int i;
            for (i = scenes.Count - 1; i >= 0; i--)
            {
                if (!scenes[i].isPopUp)
                {
                    break;
                }
            }
            for (int j = i; j < scenes.Count; j++)
            {
                scenes[j].draw(sb);
            }
        }
        public bool handleInput()
        {
            scenes[scenes.Count - 1].handleInput();
            return true;
        }
        public void markForAdd(Scene sceneToAdd)
        {
            if (canAdd) 
            {
                scenes.Add(sceneToAdd);
            }
            else
            {
                if (markedForAdd == null) markedForAdd = new List<Scene>();
                markedForAdd.Add(sceneToAdd);
            }
        }
        public void markForRemove(Scene sceneToRemove)
        {
            if (canRemove)
            {
                scenes.Remove(sceneToRemove);
            }
            else
            {
                if (markedForRemove == null) markedForRemove = new List<Scene>();
                markedForRemove.Add(sceneToRemove);
            }
        }
        #endregion
    }
}
